<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "260px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData, type } = {}) {
      if(!expectedData){
        return;
      }
      let xData = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
      xData = expectedData.map((item) => item.time);
      if(actualData){
        var mm= actualData.map((item) => item.time);
        let mergedData  = Array.from(new Set(xData.concat(mm)))
        mergedData.sort();
        xData=mergedData;
      }
      
      let a = expectedData.map((item) => item.num);

      

      // var a=expectedData
      // var b=actualData
      let b = [];
      if (type == 3) {
        b = actualData.map((item) => item.num);
      }
      var seriesd = [
        {
          name: "消费金额",
          itemStyle: {
            normal: {
              color: "#FF005A",
              lineStyle: {
                color: "#FF005A",
                width: 2,
              },
            },
          },
          smooth: true,
          type: "line",
          areaStyle: {
            color: "rgba(255, 0, 90, 0.3)", 
          },
          data: a,
          animationDuration: 2800,
          animationEasing: "cubicInOut",
        },
        {
          name: "预存金额",
          smooth: true,
          type: "line",
          areaStyle: {
            color: "rgba(0, 128, 255, 0.3)", 
          },
          itemStyle: {
            normal: {
              color: "#3888fa",
              lineStyle: {
                color: "#3888fa",
                width: 2,
              },
              areaStyle: {
                color: "#f3f8ff",
              },
            },
          },
          data: b,
          animationDuration: 2800,
          animationEasing: "quadraticOut",
        },
      ];

      if (type == 1) {
        seriesd = [
          {
            name: "服务订单",
            itemStyle: {
              normal: {
                color: "#36a3f7",
                lineStyle: {
                  color: "#36a3f7",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            areaStyle: {
              color: "rgba(54, 163, 247, 0.3)", 
            },
            data: a,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
        ];
      }

      if (type == 2) {
        seriesd = [
          {
            name: "售后订单",
            itemStyle: {
              normal: {
                color: "#34bfa3",
                lineStyle: {
                  color: "#34bfa3",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            areaStyle: {
              color: "rgba(52, 191, 163, 0.3)", 
            },
            data: a,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
        ];
      }

      this.chart.setOption({
        xAxis: {
          data: xData,
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
        },
        legend: {
          data: name,
        },
        series: seriesd,
      });
    },
  },
};
</script>
